<?php
/**
 *
 * User: Administrator
 * Date: 2018\9\28 0028
 * Time: 13:40
 */

namespace app\Lib\Ioc\Role;


use app\Lib\Exception\BlException;
use app\Lib\Model\Role\Role;
use app\Lib\Model\Role\RoleManage\AddRole;
use app\Lib\Model\Role\RoleManage\GetRoleInfo;
use app\Lib\Model\Role\RoleManage\GetRoleList;
use app\Lib\Model\Role\RoleManage\GetRoleSelectList;
use app\Lib\Model\Role\RoleManage\SaveRole;
use app\Lib\Model\User\User;
use think\Db;

class RoleManage
{
    /**
     * 新增角色
     * @param array $data
     * @return bool
     */
    public function addRole($data = []){
        Db::transaction(function ()use($data){
            $model = new AddRole();
            $model->setOpUser(new User($data['opUid']));
            $model->setName($data['name']);
            if($data['parentId'] >0){
//                throw new BlException($data['parentId'] );
                $model->setParentObj(new Role($data['parentId']));
            }
            $model->setRoleAlias($data['roleAlias']);
            $model->setAuth($data['auth']);
            $model->setIs_admin_role($data['is_admin_role']);
            $model->run();
            return true;
        });
        return true;

    }

    /**
     * 修改角色
     * @param array $data
     * @return bool
     * @throws \app\Lib\Exception\BlException
     */
    public function editRole($data = []){
//        throw new BlException(json_encode($data));
        $role = new Role($data['roleId']);
        $transaction = Db::transaction(function ()use($data,$role){
            $model = new SaveRole($role);
            $model->setOpUser(new User($data['opUid']));
            $model->setName($data['name']);
            if($data['parentId'] >0){
                $model->setParentObj(new Role($data['parentId']));
            }
            $model->setRoleAlias($data['roleAlias']);
            $model->setAuth($data['auth']);
            $model->setIs_admin_role($data['is_admin_role']);
            $model->run();
            return true;
        });
        if($transaction){
            $role->delAllCache();
        }
        return true;
    }

    /**
     * 获取列表
     * @param array $data
     * @return array
     */
    public function getList($data = []){
        $model = new GetRoleList();
        $model->setPage($data['page']);
        $model->setLimit($data['limit']);
        if(isset($data['keyword']) && $data['keyword'] && isset($data['keywordType']) && $data['keywordType']){
            $model->setKeyword($data['keyword']);
            $model->setKeywordType($data['keywordType']);
        }
        $result = $model->getList();
        return $result;
    }

    /**
     * 获取页数
     * @return array|string
     */
    public function getPage(){
        $model = new GetRoleList();
        $model->isPage(true);
        $result = $model->getList();
        return $result;
    }

    /**
     * 获取详情
     * @param array $data
     * @return array
     */
    public function getInfo($data = []){
        $role  = new Role($data['roleId']);
        $model = new GetRoleInfo($role);
        $info  = $model->run();
        return $info;
    }

    /**
     * 获取所有的角色
     * @return array
     */
    public function getRoleSelectList(){
        return (new GetRoleSelectList())->run();
    }

//    public function getRoleAuth(){
//        new Au
//    }
}